package com.k2data.k2app.service.pas14;

import com.k2data.k2app.domain.pas14.job.Instance;
import com.k2data.k2app.domain.pas14.job.Pas14Job;
import com.k2data.k2app.domain.pas14.job.ProjectIn;
import com.k2data.k2app.domain.pas14.project.PartitionInfo;
import com.k2data.k2app.domain.pas14.project.Project;
import com.k2data.k2app.domain.pas14.project.TimeObject;
import com.k2data.k2app.domain.vo.Job;
import com.k2data.k2app.util.DateFormatterUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.zip.DataFormatException;

/**
 * 预警处理
 * Created by clb on 17-4-18.
 */
@Log4j2
@Service
public class Job14Service {
	@Autowired
	private Pas14JobService pasJobService;
	@Autowired
	private Pas14ProjectService pas14ProjectService;

	public Job get(Long jobId) throws Exception {
		Pas14Job pas14job = pasJobService.get(jobId);
		Job job = parsepas14JobToJob(pas14job);
//		job = setModelInfo(job);
		return job;
	}

	public Long delete(Long projId,Long jobId) throws Exception {
		return pasJobService.delete(projId,jobId);
	}

	public List<Job> list () throws Exception {
		List<Pas14Job> pas14job = pasJobService.list();
		List<Job> jobs = parsepas14JobToJob(pas14job);
		return jobs;
	}

	public List<Job> listByParam (Map param) throws Exception {
		List<Pas14Job> pas14job = pasJobService.listByParam(param);
		List<Job> jobs = parsepas14JobToJob(pas14job);
		return jobs;
	}

	public List<Job> parsepas14JobToJob(List<Pas14Job> pas14Jobs) throws Exception {
		List<Job> jobs = new ArrayList<>();
		for(Pas14Job pas14job:pas14Jobs){
			Job job = parsepas14JobToJob(pas14job);
			jobs.add(job);
		}
		return jobs;
	}

	/**
	 * 将pas的作业转换为本地的作业对象
	 * @param pas14Job
	 * @return
	 */
	public Job parsepas14JobToJob(Pas14Job pas14Job) throws Exception {
		Job job = new Job();
		if(pas14Job == null){ return job;}
		Instance instance = pas14Job.getInstance();
		ProjectIn projectIn = pas14Job.getProject();

		if (instance.getTaskInstance().getEndTime() == null){
			job.setEndTime(null);
		} else {
			Date endTime1 = new Date(instance.getTaskInstance().getEndTime());
			LocalDateTime end = DateFormatterUtil.UDateToLocalDateTime(endTime1);
			job.setEndTime(end);
		}
		Date startTime1 = new Date(instance.getTaskInstance().getStartTime());
		LocalDateTime start = DateFormatterUtil.UDateToLocalDateTime(startTime1);
		job.setJobId(instance.getJobId());
		job.setModelId(projectIn.getProjId()+"");
		job.setProjName(projectIn.getProjName());
		job.setStatus(instance.getTaskInstance().getStatus());
		job.setStartTime(start);
		job.setHdfsPath(instance.getResultHdfsUrl());
		job.setResultStatus(instance.getTaskInstance().getResult());
		job.setMessage(instance.getTaskInstance().getMessage());
		job.setHasApi(String.valueOf(instance.isHasApi()));
		job.setResultApiUrl(instance.getResultApiUrl());
		job.setActualStmtWhere(instance.getActualStmtWhere());

		List<PartitionInfo> partitionInfo = pas14Job.getInstance().getPartitionInfo();
		if(partitionInfo !=null && partitionInfo.size()>0){
			PartitionInfo partitionInfo1 = partitionInfo.get(0);
			List<TimeObject> ts = partitionInfo1.getTs();
			if(ts.size()>0){
				TimeObject timeObject = ts.get(0);
				job.setStartTimeWhere(DateFormatterUtil.UDateToLocalDateTime(DateFormatterUtil.simpleDateFormat.parse(timeObject.getFrom())));
				job.setEndTimeWhere(DateFormatterUtil.UDateToLocalDateTime(DateFormatterUtil.simpleDateFormat.parse(timeObject.getTo())));
			}
		}
		//模板模型信息
//		job = setModelInfo(job);
		return job;
	}

	/**
	 * 给作业添加模板信息
	 * @param job
	 * @return
	 * @throws Exception
	 */
	public Job setModelInfo(Job job) throws Exception {
		String modelIdTemplate = job.getProjName().split("_")[1];
		Optional<Project> project = Optional.ofNullable(pas14ProjectService.getBasic(Long.parseLong(modelIdTemplate)));
		project.ifPresent(p -> {
			job.setModelIdTemplate(modelIdTemplate);
			job.setProjNameTemplate(project.get().getProjName());
		});
		return job;
	}
	public String getOwner(){
		return pas14ProjectService.getOwner();
	}
}
